package com.taiyi.renaissancemall.ui.main.search;

import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.Group;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.taiyi.renaissancemall.R;
import com.taiyi.renaissancemall.Utils.ActivityUtils;
import com.taiyi.renaissancemall.Utils.ToastUtils;
import com.taiyi.renaissancemall.adapter.AdapterManager;
import com.taiyi.renaissancemall.adapter.baseadapter.CommonAdapter;
import com.taiyi.renaissancemall.adapter.baseadapter.MultiItemTypeAdapter;
import com.taiyi.renaissancemall.base.BaseActivity;
import com.taiyi.renaissancemall.bean.SearchHistoryBean;
import com.taiyi.renaissancemall.bean.SearchKindBean;
import com.taiyi.renaissancemall.bean.SearchListBean;
import com.taiyi.renaissancemall.ui.main.goodsdetail.GoodsDetailActivity;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SearchActivity extends BaseActivity<SearchView, SearchPresent> implements SearchView, SpringView.OnFreshListener {


    @BindView(R.id.backIv)
    ImageView backIv;
    @BindView(R.id.titleTv)
    TextView titleTv;
    @BindView(R.id.rightTitleTv)
    TextView rightTitleTv;
    @BindView(R.id.tv)
    TextView tv;
    @BindView(R.id.topB)
    TextView topB;
    @BindView(R.id.searchTv)
    TextView searchTv;
    @BindView(R.id.comprehensiveTv)
    TextView comprehensiveTv;//综合
    @BindView(R.id.salesTv)
    TextView salesTv;//销量
    @BindView(R.id.priceTv)
    TextView priceTv;//价格
    @BindView(R.id.bvPriceTv)
    TextView bvPriceTv;//bv
    @BindView(R.id.filterTv)
    TextView filterTv;//筛选
    @BindView(R.id.searchContentEt)
    EditText searchContentEt;
    @BindView(R.id.lowPriceEt)
    EditText lowPriceEt;
    @BindView(R.id.maxPriceEt)
    EditText maxPriceEt;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.hotRecyclerView)
    RecyclerView hotRecyclerView;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.tv11)
    TextView tv11;
    @BindView(R.id.tv22)
    TextView tv22;
    @BindView(R.id.tv33)
    TextView tv33;
    @BindView(R.id.tv44)
    TextView tv44;
    @BindView(R.id.tv55)
    TextView tv55;
    @BindView(R.id.historyRecyclerView)
    RecyclerView historyRecyclerView;
    @BindView(R.id.group)
    Group group;
    @BindView(R.id.resultGroup)
    Group resultGroup;
    @BindView(R.id.searchResultRecyclerView)
    RecyclerView searchResultRecyclerView;
    @BindView(R.id.brandRecyclerView)
    RecyclerView brandRecyclerView;
    @BindView(R.id.springView)
    SpringView springView;
    @BindView(R.id.salesIv)
    ImageView salesIv;
    @BindView(R.id.priceIv)
    ImageView priceIv;
    @BindView(R.id.bvIv)
    ImageView bvIv;
    @BindView(R.id.vv)
    View vv;
    @BindView(R.id.filterLayout)
    ConstraintLayout filterLayout;
    private int pageNo = 1;
    private boolean isSelectPrice;
    private boolean isSelectBv;
    private boolean isSelectSale;
    private String sale = "";
    private String price = "";
    private String bv = "";
    @BindView(R.id.top)
    ConstraintLayout top;
    @BindView(R.id.tv_clear_search)
    TextView tvClearSearch;
    StringBuffer stringBuffer = new StringBuffer();
    List<SearchListBean.ComlistBean> data = new LinkedList<>();
    private CommonAdapter searchListAdapter;
    private String mContent = "";
    private CommonAdapter searchHistoryAdapter;
    private List<SearchHistoryBean.SearchlistBean> searchlist;
    //    private Dialog mDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    public SearchPresent initPresenter() {
        return new SearchPresent();
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
//        mImmersionBar.statusBarDarkFont(false, 0.2f);
        titleTv.setText(getString(R.string.search));
//        mDialog = new Dialog(this, R.style.MyDialog);
//        View view = LayoutInflater.from(this).inflate(R.layout.good_class_filter_dialog_layout, null);
//        mDialog.setContentView(view);
//        WindowManager.LayoutParams attributes = mDialog.getWindow().getAttributes();
//        attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
//        attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
//        attributes.verticalMargin = filterTv.getBottom();
//        mDialog.getWindow().setAttributes(attributes);
        comprehensiveTv.setSelected(true);
        springView.setHeader(new DefaultHeader(this));
        springView.setFooter(new DefaultFooter(this));
        springView.setGive(SpringView.Give.BOTH);
        springView.setType(SpringView.Type.FOLLOW);
        springView.setListener(this);
        searchListAdapter = AdapterManager.getSearchListAdapter(this, data);
        searchResultRecyclerView.setAdapter(searchListAdapter);
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(top).statusBarDarkFont(false, 0f).init();
//        mImmersionBar.fitsSystemWindows(false).statusBarColor(R.color.transparent).statusBarDarkFont(false, 0f).init();
    }

    private void update() {

        comprehensiveTv.setSelected(false);
        salesTv.setSelected(false);
        priceTv.setSelected(false);
        bvPriceTv.setSelected(false);
        filterTv.setSelected(false);
    }

    @Override
    protected void initData() {
        showProgress();
        presenter.getSearchHistory();
        presenter.getKindList();
    }

    @Override
    public void initEvent() {

    }


    @OnClick({R.id.backIv, R.id.searchTv, R.id.comprehensiveTv, R.id.salesTv
            , R.id.priceTv, R.id.bvPriceTv, R.id.filterTv, R.id.determineTv
            , R.id.tv11, R.id.tv22, R.id.tv33, R.id.tv44, R.id.tv55, R.id.tv_clear_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.backIv:
                finish();
                break;
            case R.id.tv_clear_search:
                showProgress();
                presenter.delHistoryList();
                break;
            case R.id.searchTv:
                String s = searchContentEt.getText().toString();
                if (TextUtils.isEmpty(s)) {
                    ToastUtils.showShortToast("请输入搜索内容");
                    return;
                }
                content = s;
                mContent = s;
                tvClearSearch.setVisibility(View.GONE);
                group.setVisibility(View.GONE);
                resultGroup.setVisibility(View.VISIBLE);
                showProgress();
                presenter.getSearchList();
                break;
            case R.id.comprehensiveTv:
                sale = "";
                price = "";
                bv = "";
                isSelectSale = false;
                isSelectPrice = false;
                isSelectBv = false;
                salesIv.setImageResource(R.mipmap.price_dsc_iv);
                priceIv.setImageResource(R.mipmap.price_dsc_iv);
                bvIv.setImageResource(R.mipmap.price_dsc_iv);
                salesTv.setSelected(false);
                priceTv.setSelected(false);
                bvPriceTv.setSelected(false);
                filterTv.setSelected(false);
                comprehensiveTv.setSelected(true);
                presenter.getSearchList();
                break;
            case R.id.salesTv:
                price = "";
                bv = "";
                isSelectSale = !isSelectSale;
                isSelectPrice = false;
                isSelectBv = false;
                priceIv.setImageResource(R.mipmap.price_dsc_iv);
                bvIv.setImageResource(R.mipmap.price_dsc_iv);

                if (!isSelectSale) {
                    sale = "1";
                    salesIv.setImageResource(R.mipmap.price_asc_iv);
                } else {
                    sale = "2";
                    salesIv.setImageResource(R.mipmap.price_dsc_iv);
                }
                comprehensiveTv.setSelected(false);
                priceTv.setSelected(false);
                bvPriceTv.setSelected(false);
                filterTv.setSelected(false);
                salesTv.setSelected(true);
                presenter.getSearchList();
                break;
            case R.id.priceTv:
                sale = "";
                bv = "";
                isSelectPrice = !isSelectPrice;
                isSelectSale = false;
                isSelectBv = false;
                salesIv.setImageResource(R.mipmap.price_dsc_iv);
                bvIv.setImageResource(R.mipmap.price_dsc_iv);
                if (!isSelectPrice) {
                    price = "1";
                    priceIv.setImageResource(R.mipmap.price_asc_iv);
                } else {
                    price = "2";
                    priceIv.setImageResource(R.mipmap.price_dsc_iv);
                }
                comprehensiveTv.setSelected(false);
                salesTv.setSelected(false);
                bvPriceTv.setSelected(false);
                filterTv.setSelected(false);
                priceTv.setSelected(true);
                presenter.getSearchList();
                break;
            case R.id.bvPriceTv:
                sale = "";
                price = "";
                isSelectBv = !isSelectBv;
                isSelectSale = false;
                isSelectPrice = false;
                salesIv.setImageResource(R.mipmap.price_dsc_iv);
                priceIv.setImageResource(R.mipmap.price_dsc_iv);
                if (!isSelectBv) {
                    bv = "1";
                    bvIv.setImageResource(R.mipmap.price_asc_iv);
                } else {
                    bv = "2";
                    bvIv.setImageResource(R.mipmap.price_dsc_iv);
                }
                comprehensiveTv.setSelected(false);
                salesTv.setSelected(false);
                priceTv.setSelected(false);
                filterTv.setSelected(false);
                bvPriceTv.setSelected(true);
                presenter.getSearchList();
                break;
            case R.id.filterTv:
//                update();
//                filterTv.setSelected(true);
//                mDialog.show();
                vv.setVisibility(View.VISIBLE);
                filterLayout.setVisibility(View.VISIBLE);
                break;
            case R.id.determineTv:
                vv.setVisibility(View.GONE);
                StringBuffer sb = new StringBuffer();
//                stringBuffer = new StringBuffer();
//                stringBuffer.append(content);
                filterLayout.setVisibility(View.GONE);
                for (int i = 0; i < searchKindBeanList.size(); i++) {
                    if (searchKindBeanList.get(i).isSelect())
                        sb.append("," + searchKindBeanList.get(i).getCategoryname());
                }

                content = mContent + sb.toString();
                Log.i("ssssssss", "----" + content);
                pageNo = 1;
                showProgress();
                presenter.getSearchList();
                break;
            case R.id.tv11:
                lowPriceEt.setText("0");
                maxPriceEt.setText("199");
                updateTv();
                tv11.setSelected(true);
                break;
            case R.id.tv22:
                lowPriceEt.setText("200");
                maxPriceEt.setText("499");
                updateTv();
                tv22.setSelected(true);
                break;
            case R.id.tv33:
                lowPriceEt.setText("500");
                maxPriceEt.setText("799");
                updateTv();
                tv33.setSelected(true);
                break;
            case R.id.tv44:
                lowPriceEt.setText("800");
                maxPriceEt.setText("999");
                updateTv();
                tv44.setSelected(true);
                break;
            case R.id.tv55:
                lowPriceEt.setText("1000");
                maxPriceEt.setText("");
                updateTv();
                tv55.setSelected(true);
                break;
        }
    }

    private void updateTv() {
        tv11.setSelected(false);
        tv22.setSelected(false);
        tv33.setSelected(false);
        tv44.setSelected(false);
        tv55.setSelected(false);
    }

    @Override
    public void searchHistorySuccess(SearchHistoryBean searchHistoryBean) {
        hideProgress();
        List<SearchHistoryBean.RelistBean> relist = searchHistoryBean.getRelist();
        CommonAdapter hotSearchHistoryAdapter = AdapterManager.getHotSearchHistoryAdapter(this, relist);
        hotRecyclerView.setAdapter(hotSearchHistoryAdapter);
        hotSearchHistoryAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                group.setVisibility(View.GONE);
                resultGroup.setVisibility(View.VISIBLE);
                tvClearSearch.setVisibility(View.GONE);
//                stringBuffer.append(relist.get(position).getCategoryname());
                content = relist.get(position).getCategoryname();
                mContent = relist.get(position).getCategoryname();
                showProgress();
                presenter.getSearchList();
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

        searchlist = searchHistoryBean.getSearchlist();
        searchHistoryAdapter = AdapterManager.getSearchHistoryAdapter(this, searchlist);
        historyRecyclerView.setAdapter(searchHistoryAdapter);
        searchHistoryAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                group.setVisibility(View.GONE);
                resultGroup.setVisibility(View.VISIBLE);
                tvClearSearch.setVisibility(View.GONE);
//                stringBuffer.append(searchlist.get(position).getSearchid());
                content = searchlist.get(position).getContent();
                mContent = searchlist.get(position).getContent();
                showProgress();
                presenter.getSearchList();
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }

    @Override
    public void searchHistoryFailure(String msg) {
        hideProgress();
        ToastUtils.showLongToast(msg);
    }

    @Override
    public void getSearchListSuccess(SearchListBean searchListBean) {
        hideProgress();
        springView.onFinishFreshAndLoad();
        List<SearchListBean.ComlistBean> comlist = searchListBean.getComlist();
        if (pageNo == 1) {
            data.clear();
        }
        data.addAll(comlist);
        searchListAdapter.notifyDataSetChanged();
        Log.i("DASDASDADASA", "-----" + comlist);

        searchListAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("commodityId", comlist.get(position).getCommodityid());
                ActivityUtils.next(SearchActivity.this, GoodsDetailActivity.class, bundle);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }

    @Override
    public void getSearchListFailure(String msg) {
        hideProgress();
        toast(msg);
    }

    @Override
    public void delHistoryListSuccess() {
        presenter.getSearchHistory();
    }

    @Override
    public void delHistoryListFailure() {
        hideProgress();
    }

    @Override
    public String getPageNo() {
        return String.valueOf(pageNo);
    }

    @Override
    public String getCategoryId() {
        return stringBuffer.toString();
    }

    @Override
    public String getSaleValue() {
        return sale;
    }

    @Override
    public String getPriceValue() {
        return price;
    }

    @Override
    public String getBvValue() {
        return bv;
    }

    private List<SearchKindBean> searchKindBeanList = new ArrayList<>();

    @Override
    public void getKindListSuccess(List<SearchKindBean> list) {
        searchKindBeanList.clear();
        brandRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        searchKindBeanList.addAll(list);
        CommonAdapter searchKindListAdapter = AdapterManager.getSearchKindListAdapter(this, searchKindBeanList);
        brandRecyclerView.setAdapter(searchKindListAdapter);

    }

    private String minPrice = "";

    private String maxPrice = "";


    @Override
    public String getStartPrice() {
        String s = lowPriceEt.getText().toString();
        if (!TextUtils.isEmpty(s)) {
            minPrice = s;
        }
        return minPrice;
    }

    private String content = "";

    @Override
    public String getEndPrice() {
        String s = maxPriceEt.getText().toString();
        if (!TextUtils.isEmpty(s)) {
            maxPrice = s;
        }

        return maxPrice;
    }

    @Override
    public String getSearchContent() {
        return content;
    }

    @Override
    public void onRefresh() {
        pageNo = 1;
        presenter.getSearchList();
    }

    @Override
    public void onLoadmore() {
        pageNo++;
        presenter.getSearchList();
    }
}
